#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <stdarg.h>
using namespace std;

void Usage(std::string proc)
{
    std::cout<<"\n\rUsage: "<<proc<<"port\n"<<std::endl;
}
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        exit(1);
    }

    string serverip=argv[1];
    uint16_t serverport=stoi(argv[2]);

    struct sockaddr_in server;
    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(serverport);
    server.sin_addr.s_addr=inet_addr(serverip.c_str());
    socklen_t len=sizeof(server);

    int sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd<0)
    {
        perror("socket:");
        exit(1);
    }

    char buffer[1024];
    string message;
    while(true)
    {
        cout<<"please enter@ "<<endl;
        getline(cin,message);

        sendto(sockfd,message.c_str(),message.size(),0,(struct sockaddr*)&server,len);

        struct sockaddr_in temp;
        socklen_t len=sizeof(temp);
        size_t s=recvfrom(sockfd,buffer,1023,0,(struct sockaddr*)&temp,&len);
        if(s>0)
        {
            buffer[s]=0;
            //cout<<buffer<<endl;
        }
    }

    close(sockfd);
    return 0;
}